<template>
  <div class="satisfaction-detail">
    <div class="satisfaction-detail__select">
      <div class="satisfaction-detail__select-city">
        <el-select v-model="city" size="mini" @change="getData">
          <el-option
            v-for="(item, i) in cityList"
            :key="i"
            :label="item"
            :value="item"
            >
          </el-option>
        </el-select>
      </div>
      <div class="satisfaction-detail__select-type">
        <el-select v-model="type" size="mini" @change="getData">
          <el-option
            v-for="(item, i) in typeList"
            :key="i"
            :label="item.type"
            :value="item.id"
            >
          </el-option>
        </el-select>
      </div>
      <div class="satisfaction-detail__select-date" v-if="type===0">
        <el-select v-model="dataQuarterStr" size="mini" @change="getData">
          <el-option
            v-for="item in dateList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            >
          </el-option>
        </el-select>
      </div>
      <div class="satisfaction-detail__select-date" v-if="type===1">
        <el-date-picker
          v-model="dayTime"
          type="month"
          :clearable="false"
          size="mini"
          value-format="yyyy-MM"
          format="yyyy年M月"
          @change="getData">
        </el-date-picker>
      </div>
    </div>
    <div class="satisfaction-detail__content">
      <ve-histogram :data="chartData" :settings="chartSettings" :extend="chartExtend" :width="width" height="13rem" :colors="colors"></ve-histogram>
    </div>
  </div>
</template>
<script>
import { Select, Option, DatePicker } from 'element-ui'
import Histogram from 'v-charts/lib/histogram.common'
import { getKhmydZone } from 'src/api/api'
import storage from 'good-storage'
import { formatFontSize, formatDate } from 'js/utils'
export default {
  components: {
    [Select.name]: Select,
    [Option.name]: Option,
    [Histogram.name]: Histogram,
    [DatePicker.name]: DatePicker,
  },
  data(){
    this.colors = ['#0E962A', '#E56F82']
    return {
      width: '95%',
      cityList: storage.get('cityList'),
      city: '杭州',
      dayTime: formatDate('yyyy-mm', new Date()),
      dateList: [
        {
          value: '2016_0',
          label: '2016年1-6月'
        },
        {
          value: '2016_1',
          label: '2016年7-12月'
        },
        {
          value: '2017_0',
          label: '2017年1-6月'
        },
        {
          value: '2017_1',
          label: '2017年7-12月'
        },
        {
          value: '2018_0',
          label: '2018年1-6月'
        },
        {
          value: '2018_1',
          label: '2018年7-12月'
        },
        {
          value: '2019_0',
          label: '2019年1-6月'
        },
        {
          value: '2019_1',
          label: '2019年7-12月'
        },
        {
          value: '2020_0',
          label: '2020年1-6月'
        },
        {
          value: '2020_1',
          label: '2020年7-12月'
        },
      ],
      dataQuarterStr: '2020_0',
      typeList: [
        {
          type: '按半年度',
          id: 0
        },
        {
          type: '按月度',
          id: 1
        },
      ],
      type: 0,
      chartData: {
        columns: ['zoneName', 'khmyd','citymyd'],
        rows: []
      },
      chartSettings: {
        showLine: ['citymyd'],
        labelMap: {
          'khmyd': '满意度',
          'citymyd': '满意度达标线'
        },
      },
      chartExtend: {
        legend: {
          show: true,
          textStyle: {
            color: '#fff',
            fontSize: formatFontSize(0.1),
          },
          selectedMode: false,
          left: '10%',
          top: '5%',
          itemWidth: formatFontSize(0.13),
          itemHeight: formatFontSize(0.08),
        },
        xAxis: {
          axisLabel: {
            color: '#8EC7DC',
            fontSize: formatFontSize(0.08),
            rotate: 20
            // formatter: param => {
              
            // }
          },
        },
        yAxis: {
          axisLabel: {
            formatter: '{value}%',
            color: '#fff',
            fontSize: formatFontSize(0.1),
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#fff',
              opacity: 0.1,
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: '#fff',
              opacity: 0.1,
            }
          },
          max: 100,
          splitNumber: 10,
        },
        grid: {  
          left: '3%',  
          right: '0',
          top: '20%',
          bottom: '0',  
        },
        series: {
          barWidth: formatFontSize(0.2),
          label: { 
            show: true, 
            position: 'top',
            color: '#fff',
            fontSize: formatFontSize(0.1),
            // formatter: '{c}%',
            formatter: param => {
              let target = ''
              if(param.seriesType == 'bar') {
                target = param.data + '%'
              }else if(param.seriesType == 'line') {
                target = ''
              }
              return target
            }
          },
        }
      },
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      let params = {}
      if(this.type === 0) {
        params = {
          city: this.city,
          dataQuarterStr: this.dataQuarterStr
        }
      } else if(this.type === 1) {
        params = {
          city: this.city,
          dayTime: this.dayTime
        }
      }
      getKhmydZone(params).then(res=> {
        this.chartData.rows = res.data
        if(res.data.length > 5) {
          this.width = res.data.length * 60 + 'px'
        } else {
          this.width = '95%'
        }
      })
    }
  }
}
</script>
<style lang="scss">
.satisfaction-detail {
  &__select {
    text-align: right;
    padding: 5px 10px 5px 0;
    font-size: 14px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    color: #AAECFF;
    .el-select, .el-date-editor{
      .el-input__inner {
        // width: 150px;
        border: 0;
        border-radius: 0;
        background:rgba(11,21,58,1);
        box-shadow:1px 1px 0px 0px rgba(255,255,255,0.25), 0px 2px 4px 0px rgba(0, 0, 0, 1);
        color: #AAECFF;
        font-size: 14px;
      }
      .el-input__icon {
        color: #AAECFF;
        font-size: 14px;
      }
    }
    &-city {
      margin-right: 20px;
      .el-select {
        width: 80px;
      }
    }
    // &-date {
    //   margin-right: 20px;
    //   .el-select {
    //     width: 120px;
    //   }
    // }
    &-date {
      margin-right: 20px;
      .el-select {
        width: 120px;
      }
      .el-date-editor {
        width: 120px;
      }
      .el-input__prefix ,.el-input__suffix{
        display: none;
      }
      .el-input--prefix .el-input__inner {
        padding-left: 5px;
      }
      .el-input--suffix .el-input__inner {
          padding-right: 0px;
      }
    }
    &-type {
      margin-right: 20px;
      .el-select {
        width: 120px;
      }
    }
  }
  &__content {
    overflow: scroll;
  }
}
</style>